package com.copro.simple.db;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.ihelpuoo.orm.handler.LoopScope;
import org.ihelpuoo.util.Pager;

public abstract class QueryListCallBack implements QueryCallback {

	private Pager pager;

	protected QueryListCallBack(Pager pager) {
		this.pager = pager;
	}

	protected QueryListCallBack() {
		this(null);
	}

	@Override
	public void exec(ResultSet rs) throws SQLException {
		if (rs.last()) {
			if (null != pager)
				if (pager.getPageSize() > 1000)
					rs.setFetchSize(20);
				else
					rs.setFetchSize(pager.getPageSize());
			LoopScope ls = LoopScope.evaluate(pager, rs.getRow());
			if (rs.absolute(ls.start + 1))
				for (int i = ls.start; i < ls.max; i++) {
					makeObject(rs);
					if (!rs.next())
						break;
				}
		}
	}

	abstract protected void makeObject(ResultSet rs) throws SQLException;

}
